import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '@/views/login'
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'
// import { getTokena } from '@/utils/save.js'
import { Message } from 'element-ui'
import store from '../store/index'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    //   重定向
    redirect: '/login'
  },
  {
    path: '/login',
    component: login
  },
  {
    path: '/layout',
    component: () => import('@/views/layout/index.vue'),
    children: [
      {
        nmae: 'user',
        path: '/layout/user',
        component: () => import('@/views/user')
      },
      {
        nmae: 'subjectList',
        path: '/layout/subjectList',
        component: () => import('@/views/subjectList/index.vue')
      },
      {
        nmae: 'enterprise',
        path: '/layout/enterprise',
        component: () => import('@/views/enterprise/index.vue')
      },
      {
        nmae: 'question',
        path: '/layout/question',
        component: () => import('@/views/question/index.vue')
      },
      {
        nmae: 'dataOverview',
        path: '/layout/dataOverview',
        component: () => import('@/views/dataOverview/index.vue')
      }
    ]
  }
]
const router = new VueRouter({
  routes
})

// 访问的白名单, 可以直接通行
const whiteList = ['/login']
router.beforeEach((to, from, next) => {
  // 访问的路径在白名单
  Nprogress.start()
  if (whiteList.includes(to.path.toLowerCase())) {
    next()
  } else {
    if (store.state.login.token) {
      next()
    } else {
      Message.error('你还没有登录')
      next('/login')
    }
  }
  Nprogress.done()
})
router.afterEach((to, from) => {
  setTimeout(() => {
    Nprogress.done()
  }, 1500)
})

export default router
